<?php

namespace app\index\controller\tan;

use think\Controller;
use think\Request;
use app\index\controller\Sms\AliYunSms;
use app\index\model\tan\PhoneCode;
use think\Session as session;

/**
 * 
 * 
 * 用户的店铺添加
 */
class Business extends Controller
{
    private $rule=[
                "business_name"=>"require|max:20",
                "business_country"=>"max:30",
                "business_province"=>"max:20",
                "business_city"=>"max:10",
                "business_address"=>"require|max:128",
                "license_image_url"=>"require|max:225",
                "start_business_hours"=>"require|max:32",
                "stop_business_hours"=>"require|max:32",
                "store_description"=>"max:225"
            ];
    /**
     * 门店列表
     *
     * @return \think\Response
     */
    public function index(\app\index\model\tan\Business $bu)
    {
        ReturnData::ok($bu->showlist(session::get("user.id")));
     
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
    */
    public function create()
    {
        //
    }

    

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
        
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request,\app\index\model\tan\Business $bu, $id=0)
    {
        ReturnData::IfTrue(  $this->validate($request->post(),$this->rule), 10021);
        $d=$bu->where([
                "id"=>$id,
                "business_id"=>session::get("user.id")
            ])
            ->limit(1)
            ->update(
                Eliminate::E($request->post(), $this->rule)
            );
//        echo $d;
//            var_dump($d);
        ReturnData::exit_($d, 10022,[],"更新失败,原因可能是无任何修改");
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(\app\index\model\tan\Business $bu,$id)
    {
        //
            $d=$bu->where([
                "id"=>$id,
                "business_id"=>session::get("user.id")
            ])
            ->limit(1)
            ->update([
                "state"=>1
            ]);
        ReturnData::exit_($d, 10023,[],"更新失败,原因可能是无任何修改");
    }
    /**
     * 保存添加的店铺
     * @param Request $request
     * @param PhoneCode $pc 验证码表
     * @param \app\index\model\tan\Business $bu 店铺表
     * @param type $mobile  电话
     * @param type $code    验证码
     */
    public function save(Request $request ,PhoneCode $pc,\app\index\model\tan\Business $bu ,$mobile,$code=""){
        ReturnData::bo($code, 10005);
        $data= $pc->where([
                'phone'=>$mobile,
                "code"=>$code ,
                "user_id"=> session::get("user.id") 
            ] ) ->find();//->toArray();
        ReturnData::bo($data, 10005,[],"验证码错误");
        $Data=$request->post();
        
        $bool=$this->validate($Data ,  $this->rule);
        if($bool !== true){
            ReturnData::err(10006,$bool);
        }
        $bu->savebusiness(Eliminate::E($Data, $this->rule) ,$mobile);
        ReturnData::ok();
    }
    /**
     * 发送验证码
     * @param AliYunSms $sms    阿里云
     * @param PhoneCode $pc 电话验证码 表 model
     * @param mobile $mobile  电话号码
     * @return json
     */
    public function sms(AliYunSms $sms, PhoneCode $pc,$mobile){
        $rund= rand(1000, 9999);
        $data= $sms ->code_type($rund,"reg")->setRecNum($mobile);
        if($data['code'] == 666){
            $pc->msg="reg";
            $pc->user_id =session::get("user.id");
            $pc->createtime =date("Y-m-d H:i:s");
            $pc->code=$rund;
            $pc->phone=$mobile;
            $pc->save();
        }
       return $data;
    }
}
